package com.btpj.blog_server.controller;

import com.btpj.blog_server.entity.Article;
import com.btpj.blog_server.entity.BaseResp;
import com.btpj.blog_server.entity.Tag;
import com.btpj.blog_server.entity.ViewCount;
import com.btpj.blog_server.service.ArticleService;
import com.btpj.blog_server.utils.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @GetMapping("/all")
    public BaseResp<List<Article>> getAllArticlesByState(Integer state, String keywords,
                                                         @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(value = "pageCount", defaultValue = "6") Integer pageCount) {
        BaseResp<List<Article>> baseResp = new BaseResp<>();
        baseResp.setStatus(0);
        baseResp.setMsg("获取文章列表成功");
        baseResp.setPage(page);
        baseResp.setData(articleService.getAllArticlesByState(state, keywords, page, pageCount, false));
        baseResp.setPageCount(pageCount);
        baseResp.setTotalCount(articleService.getArticleCountByState(state, keywords, false));
        return baseResp;
    }

    @GetMapping("/details")
    public BaseResp<Article> getArticleById(Long id) {
        Article article = articleService.getArticleById(id);
        if (article == null) {
            return new BaseResp<>(1, "获取文章详情失败");
        } else {
            List<Tag> tagList = article.getTags();
            String[] tagArr = new String[tagList.size()];
            for (int i = 0; i < tagList.size(); i++) {
                tagArr[i] = tagList.get(i).getName();
            }
            article.setTagNames(tagArr);
            return new BaseResp<>(0, "获取文章详情成功", article);
        }
    }

    @GetMapping("/delete")
    public BaseResp<Void> deleteArticlesById(Long[] articleIds) {
        if (articleService.deleteArticlesById(articleIds) == articleIds.length) {
            return new BaseResp<>(0, "文章删除成功");
        } else {
            return new BaseResp<>(1, "文章删除失败");
        }
    }

    @GetMapping("/update")
    public BaseResp<Void> updateArticlesStateById(Long[] articleIds, Integer state) {
        if (state < 0 || state > 2) {
            return new BaseResp<>(1, "文章状态操作有误");
        }
        if (articleService.updateArticlesStatesById(articleIds, state) == articleIds.length) {
            return new BaseResp<>(0, "文章状态设置成功");
        } else {
            return new BaseResp<>(1, "文章状态设置失败");
        }
    }

    /**
     * 保存文章
     *
     * @param article 文章 必传属性（title、mdContent、htmlContent、categoryId、state）可选（tags）
     * @return BaseResp
     */
    @PostMapping("/add")
    public BaseResp<Void> addArticle(Article article) {
        if (article.getTitle() == null || article.getTitle().trim().isEmpty()) {
            return new BaseResp<>(1, "文章必须要有标题，请先添加标题");
        }
        if (article.getMdContent() == null || article.getMdContent().trim().isEmpty()) {
            return new BaseResp<>(1, "文章必须要有内容，请先添加内容");
        }
        if (article.getCategoryId() == null) {
            return new BaseResp<>(1, "文章必须要有分类，请先选择分类");
        }
        if (articleService.addArticle(article) == 1) {
            return new BaseResp<>(0, article.getState() == 0 ? "文章保存成功" : "文章发表成功");
        } else {
            return new BaseResp<>(1, article.getState() == 0 ? "文章保存失败" : "文章发表失败");
        }
    }

    /**
     * 编辑文章
     *
     * @param article 文章 必传属性（title、mdContent、htmlContent、categoryId、state）可选（tags）
     * @return BaseResp
     */
    @PostMapping("/edit")
    public BaseResp<Void> updateArticle(Article article) {
        if (article.getTitle() == null || article.getTitle().trim().isEmpty()) {
            return new BaseResp<>(1, "文章必须要有标题，请先添加标题");
        }
        if (article.getMdContent() == null || article.getMdContent().trim().isEmpty()) {
            return new BaseResp<>(1, "文章必须要有内容，请先添加内容");
        }
        if (article.getCategoryId() == null) {
            return new BaseResp<>(1, "文章必须要有分类，请先选择分类");
        }
        if (articleService.updateArticle(article) == 1) {
            return new BaseResp<>(0, article.getState() == 0 ? "文章保存成功" : "文章发表成功");
        } else {
            return new BaseResp<>(1, article.getState() == 0 ? "文章保存失败" : "文章发表失败");
        }
    }

    @GetMapping("/viewCount")
    public BaseResp<List<ViewCount>> getViewCountsByUserId() {
        return new BaseResp<>(0, "请求成功", articleService.getViewCountsByUserId(UserManager.getCurrentUser().getId()));
    }
}
